<template>
  <div class="menu">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button @click="$router.push({ name: 'menu-create' })">添加菜单</el-button>
      </div>
      <el-table
        :data="allMenuInfo"
        border
        style="width: 100%">
        <el-table-column
          fixed
          align="center"
          type="index"
          label="编号"
          width="100">
        </el-table-column>
        <el-table-column
          align="center"
          prop="name"
          label="菜单名称">
        </el-table-column>
        <el-table-column
          prop="level"
          align="center"
          label="菜单级数">
        </el-table-column>
        <el-table-column
          prop="icon"
          align="center"
          label="前端图标">
        </el-table-column>
        <el-table-column
          prop="orderNum"
          align="center"
          label="排序">
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="250">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)"
              type="text">编辑</el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { getAllMenu, deleteMenu } from '@/services/menu'
export default {
  name: 'MenuIndex',
  data () {
    return {
      allMenuInfo: []
    }
  },
  created () {
    // 加载所有菜单信息
    this.loadAllMenuInfo()
  },
  methods: {
    handleEdit (rowData) {
      // 设置跳转
      this.$router.push({
        name: 'menu-edit',
        params: {
          id: rowData.id
        }
      })
    },
    handleDelete (rowData) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data } = await deleteMenu(rowData.id)
        if (data.code === '000000') {
          this.$message('删除成功!')
          // 更新数据列表
          this.loadAllMenuInfo()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async loadAllMenuInfo () {
      // 请求菜单数据
      const { data } = await getAllMenu()
      if (data.code === '000000') {
        this.allMenuInfo = data.data
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
